package com.hoperun.checkcloth.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hoperun.checkcloth.databinding.ItemFlawNumberBinding
import com.hoperun.checkcloth.net.ClothFlawInfo

class BoltFlawNumberAdapter : RecyclerView.Adapter<BoltFlawNumberVM>() {
    private val dataSet: MutableList<ClothFlawInfo> = mutableListOf()
    var onItemDeleteClick: ((ClothFlawInfo) -> Unit)? = null
    var onIemSetCountClick: ((ClothFlawInfo) -> Unit)? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BoltFlawNumberVM {
        val binding = ItemFlawNumberBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return BoltFlawNumberVM(binding)
    }

    override fun onBindViewHolder(holder: BoltFlawNumberVM, position: Int) {
        holder.binding.info = dataSet[position]
        holder.binding.onDeleteClick = View.OnClickListener {
            onItemDeleteClick?.invoke(dataSet[position])
        }
        holder.binding.onSetCountClick = View.OnClickListener {
            onIemSetCountClick?.invoke(dataSet[position])
        }
    }

    override fun getItemCount(): Int = dataSet.size

    fun setDataSet(list: List<ClothFlawInfo>) {
        dataSet.clear()
        dataSet.addAll(list)
        notifyDataSetChanged()
    }
}

class BoltFlawNumberVM(val binding: ItemFlawNumberBinding) : RecyclerView.ViewHolder(binding.root)

